package com.fpswork.util;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fpswork.core.exception.exception.ServiceException;
import com.fpswork.core.model.ExcelEntity;
import com.fpswork.core.model.ExcelShell;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
/**
 * i 第几行写入
 * data 写入数据
 * fileName 文件名字
 * @author Spf
 * @version
 */
public  class ExcelUtil {
	public  static void DownExecl(HttpServletResponse res,HttpServletRequest req,ExcelEntity excelEntity) throws ServiceException, UnsupportedEncodingException, WriteException{			   
		WritableWorkbook book = null;
		res.reset();
		res.setCharacterEncoding("UTF-8");// 设置字符集
	    // 创建工作流
	    OutputStream os = null;
		String fileName = new String(excelEntity.getFilename().getBytes("UTF-8"),"ISO8859-1");
	    try {
	        // 设置弹出对话框
	        res.setContentType("application/DOWLOAD");
	        res.setCharacterEncoding("UTF-8");
	        // 设置工作表的标题
	        res.setHeader("Content-Disposition", "attachment; filename="+fileName+"");// 设置生成的文件名字
	        os = res.getOutputStream();
	        // 初始化工作表
	        book = Workbook.createWorkbook(os);
	    } catch (IOException e1) {
	        throw new ServiceException("导出失败");
	    }
	    try {
	    	List<ExcelShell> filedatas = new ArrayList<ExcelShell>();
	    	filedatas = excelEntity.getFiledata();
	    	for(int i=0;i<filedatas.size();i++){
	    	ExcelShell filedata = new ExcelShell();
	     	filedata= filedatas.get(i);
	        // 以下为excel表格内容
	    	//excel的sheet名
	        WritableSheet sheet = book.createSheet(filedata.getShellname(), i);
	        // 表字段名
	      	int titletemp=0;
	        for (String title : filedata.getTitlemap().keySet()) { 
	        	Object val=filedata.getTitlemap().get(title);
	            sheet.addCell(new jxl.write.Label(titletemp, 0, val.toString()));
	            sheet.setColumnView(titletemp, 40); //列宽  
	            titletemp++;
	        }
	        List<Map> data = filedata.getData();
	        // 将数据追加
	        if(null!=data){
                for(int k=0;k<data.size();k++){
                	Map<String, Object> map=data.get(k);            	
                	int x=0;
                	for (String title : filedata.getTitlemap().keySet()) { 
          	          	Object val=map.get(title);
	          	        if(null==val){
	          	        	val=0;
	          	        }            		
	              		Label label = new Label(x,filedata.getShellflag()+k,val.toString());
	                      sheet.addCell(label); 
	                      x++;
	          	        }
                	for (String key : map.keySet()) { 
                		
                	}  
                }
            }
	    	}
	        book.write();
	        book.close();
	    } catch (Exception e) {
	        throw new ServiceException("导出失败");
	    } finally {
	        if (null != os) {
	            try {
	                os.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	    }
	}
}
